<?php
/**
 * create by akon
 * date: 2016年12月4日18:20:29
 * 后台首页控制器
 */
namespace Manager\Controller;

use Think\Controller;

class IndexController extends BaseController
{
    /**iframe框架集成**/
    public function index()
    {
        //获得登录用户名
        $adminModel = new \Manager\Model\AdminModel();
        $adminInfo = $adminModel->getAdminInfo(session('admin_id'));
        $this->assign("admin_info", $adminInfo);
        $this->display();
    }

    /**后台首页欢迎**/
    public function welcome()
    {
        $this->assign("sys_info", $this->get_sys_info());
        $this->display();
    }

    /**系统信息**/
    public function get_sys_info()
    {
        $sys_info = array(
            'os' => PHP_OS,                                                                                   //服务器操作系统
            'zlib' => function_exists('gzclose') ? 'YES' : 'NO',                                                //zlib支持
            'safe_mode' => (boolean)ini_get('safe_mode') ? 'YES' : 'NO',                                             //安全模式
            'timezone' => function_exists("date_default_timezone_get") ? date_default_timezone_get() : 'no_timezone',
            'curl' => function_exists("curl_init") ? 'YES' : 'NO',                                              //curl扩展
            'web_server' => $_SERVER['SERVER_SOFTWARE'],                                                              //服务器环境
            'phpv' => phpversion(),                                                                             //php版本
            'ip' => getHostByName($_SERVER['SERVER_NAME']),                                                   //ip地址
            'fileupload' => @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown',                    //文件上传限制
            'max_ex_time' => @ini_get('max_execution_time') . 's',                                                     //脚本执行最大时间
            'domain' => $_SERVER['HTTP_HOST'],                                                                    //服务器域名
            'memory_limit' => ini_get('memory_limit'),                                                                  //最大占用内存
            'version' => file_get_contents('./Akon/Manager/Conf/version.txt'),                                     //应用版本
        );
        $mysqlInfo = M()->query("SELECT VERSION() as version");
        $sys_info['mysql_version'] = $mysqlInfo[0]['version'];                                                          //数据库版本
        if (function_exists('gd_info')) {
            $gd = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }

        return $sys_info;
    }

    /**网站菜单**/
    public function menu()
    {
        $menu = D("Menu")->getMenu();
        $menu_tree = unlimitedForLevel($menu);
        $this->assign("info", $menu_tree);
        $this->display();
    }

    /**网站菜单添加及编辑**/
    public function menu_operate()
    {
        $id = I("id");
        //异步接收数据
        $menu = I("post.info");
        if ($menu && is_array($menu)) {
            //1、添加数据
            if (!$menu['id']) {
                $res = D("Menu")->menuAdd($menu);
                if ($res) {
                    $this->ajaxReturn(array('status' => 1, 'msg' => '菜单添加成功'));
                } else {
                    $this->ajaxReturn(array('status' => 0, 'msg' => '菜单添加失败'));
                }
            } else {
                //2、编辑数据
                $parent_id = I("parent_id");
                $res = D("Menu")->editCurrentMenu($menu, $parent_id);
                if ($res) {
                    $this->ajaxReturn(array('status' => 1, 'msg' => '菜单编辑成功'));
                } else {
                    $this->ajaxReturn(array('status' => 0, 'msg' => '菜单编辑失败'));
                }
            }
        } else {
            //获得所有菜单
            if ($id && $id > 0) {
                //编辑
                $menu = D("Menu")->getNoMy($id);
            } else {
                //添加
                $menu = D("Menu")->getMenu();
            }
            $menu_tree = unlimitedForLevel($menu);
            //当前菜单数据
            $current_menu = D("Menu")->getCurrentMenu($id);
            $this->assign("info", $current_menu);
            $this->assign("menu", $menu_tree);
            $this->display();
        }
    }

    /**删除菜单**/
    public function menu_del()
    {
        $id = I("id");
        $res = D("Menu")->delMenu($id);
        if ($res) {
            $info = array(
                'status' => 1,
                'msg' => '删除成功'
            );
        } else {
            $info = array(
                'status' => 0,
                'msg' => '删除失败'
            );
        }
        $this->ajaxReturn($info);
    }

    /**更新is_open字段**/
    public function updateField()
    {
        $res = M("menu")->where("id=" . I("id"))->setField("is_open", I("value"));
        return $this->ajaxReturn($res);
    }

    /**更新排序字段**/
    public function updateSortField()
    {
        $res = M("menu")->where("id=" . I("id"))->setField("sort", I("value"));
        return $this->ajaxReturn($res);
    }

    /**批量操作**/
    public function batchUpdate()
    {
        $operation_status = I("selectValue");
        $menu_ids = I("menuIdArr");
        $menu_idsStr = implode(',', $menu_ids);
        if ($operation_status == 1) {
            //1、开启
            $res = D("Menu")->batchOpening($menu_idsStr);
            if ($res) {
                $resToView = array('status' => 1);
            } else {
                $resToView = array('status' => 0);
            }
        } else if ($operation_status == 2) {
            //2、取消
            $res = D("Menu")->batchCancel($menu_idsStr);
            if ($res) {
                $resToView = array('status' => 1);
            } else {
                $resToView = array("status" => 0);
            }
        } else {
            //3、删除
            $res = D("Menu")->batchDel($menu_idsStr);
            if ($res) {
                $resToView = array("status" => 1);
            } else {
                $resToView = array("status" => 0);
            }
        }
        $this->ajaxReturn($resToView);
    }
}